iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 10
0
Mobile Development

Android開發系列 第 10

[Day10] 簡單的SeekBar_2

  • 分享至 

  • xImage
  •  

大家好今天繼續來示範SeekBar的其他用法,那我們今天會示範到自定義的SeekBar以及使用SeekBar來控制媒體音量的大小,廢話不多說那我們就開始吧!

首先我們先在res -> drawable新增兩個xml,一個是設定進度條的樣式,另一個是設定拖動點的樣式,代碼如下:

seekbar_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:id="@+id/backgroung">
        <shape>
            <solid android:color="#65686A"/>
        </shape>
    </item>

    <item android:id="@+id/notpogress">
        <clip>
            <shape>
                <solid android:color="#ff51495e"/>
            </shape>
        </clip>
    </item>

    <item android:id="@+id/progress">
        <clip>
            <shape>
                <solid android:color="#FF5722"/>
            </shape>
        </clip>
    </item>

</layer-list>

seekbar_circle.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">

    <solid
        android:color="#A033FF"/>

    <size
        android:width="20dp"
        android:height="20dp"/>

</shape>

接下來是activity_main.xml,設定了一個SeekBar和一個TextView,使用progressDrawabe設置SeekBar進度條樣式,使用thumb設置拖動點樣式。

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <SeekBar
        android:id="@+id/seekbar"
        style="@android:style/Widget.Material.SeekBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:progressDrawable="@drawable/seekbar_bg"
        android:thumb="@drawable/seekbar_circle"
        android:visibility="visible"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.424" />

    <TextView
        android:id="@+id/textview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="25sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/seekbar" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity

import androidx.appcompat.app.AppCompatActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioManager;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    AudioManager audioManager;
    SeekBar seekBar;
    VolumeReceiver volumeReceiver;
    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        registerReceiver();

        textView = findViewById(R.id.textview);

        audioManager = (AudioManager)getSystemService(AUDIO_SERVICE);
        seekBar = findViewById(R.id.seekbar);

        int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        seekBar.setMax(maxVolume);

        int progressVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        seekBar.setProgress(progressVolume);
        textView.setText("當前媒體音量大小為:"+progressVolume);

        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (fromUser){
                    audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,progress,0);
                    int progressVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC );
                    seekBar.setProgress(progressVolume);
                    textView.setText("當前媒體音量大小為:"+progressVolume);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

    }

    private void registerReceiver(){
        volumeReceiver = new VolumeReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction("android.media.VOLUME_CHANGED_ACTION") ;
        this.registerReceiver(volumeReceiver,filter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(volumeReceiver);
    }

    private class VolumeReceiver extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals("android.media.VOLUME_CHANGED_ACTION")){
                int progressVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
                seekBar.setProgress(progressVolume);
                textView.setText("當前媒體音量大小為:"+progressVolume);
            }
        }
    }

}
        int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        seekBar.setMax(maxVolume);

        int progressVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        seekBar.setProgress(progressVolume);

maxVolume 是拿取到媒體音量的最大值
progressVolume 是拿取到目前媒體音量的大小
然後我們使用.setMax以及.setProgress來設定seekbar的最大音量以及進度。

然後我們註冊廣播接收器,用來接收當使用手機音量按鍵時同步更改app seekbar的進度條。

private void registerReceiver(){
        volumeReceiver = new VolumeReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction("android.media.VOLUME_CHANGED_ACTION") ;
        this.registerReceiver(volumeReceiver,filter);
    }
 private class VolumeReceiver extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals("android.media.VOLUME_CHANGED_ACTION")){
                int progressVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
                seekBar.setProgress(progressVolume);
                textView.setText("當前媒體音量大小為:"+progressVolume);
            }
        }
    }

當我們離開app時我們要取消註冊,於是我使用了unregisterReceiver來取消註冊。

@Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(volumeReceiver);
    }

那我們今天就示範到這了,謝謝大家的觀看。


上一篇
[Day9] 簡單的SeekBar_1
下一篇
[Day11] 簡單的CheckBox(單選)
系列文
Android開發30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言